﻿using System.Text.RegularExpressions;
using com.fukefu.source;
using com.fukefu.tools;

namespace com.fukefu.info.b2b.auto
{
    public class QQMA : Information
    {
        int currentID = 0;

        public override bool Login(string name, string pass, string vcode)
        {
            ParametersData pd = new ParametersData();
            pd.Add("yhm", name);
            pd.Add("mm", pass);
            string html = source.Post("http://www.qqma.com/mdb/login789x.asp", pd.Read(), "http://www.qqma.com/");
            if (html.IndexOf("<dd><a href=\"/mdb/login_out.asp\" title=\"安全退出登录\">安全退出登录</a></dd>") != -1)
            {
                return true;
            }
            return false;
        }

        public override InformationResult PostArticle(string title, string content, string vcode)
        {
            InformationResult result = new InformationResult();
            if (currentID == 0)
            {
                result = PostProductBusinessOpportunities(title, content);
            }
            else
            {
                result = PostProductsShow(title, content);
            }
            return result;
        }
        private InformationResult PostProductsShow(string title, string content)
        {
            InformationResult info = new InformationResult();
            string html = "";
            SourceFormData sfd = new SourceFormData("gb2312");
            //头部数据
            sfd.Write("cp", title);
            sfd.Write("pp", pif.Get("公司"));
            sfd.Write("RootCategory", site.TypeId[0]);
            sfd.Write("sshy", site.TypeId[1]);
            sfd.Write("ms", MyFunction.ToText(content));
            //发送文件
            sfd.WriteFile("file1", RandomPicture.Get());
            //尾部数据
            sfd.Write("submitPost", "一切完成，我要发布！");
            //结尾
            html = source.Post("http://www.qqma.com/mdb/cpzs_asaveC2b1318I17.asp", sfd.GetContentType(), sfd.Read(), "http://www.qqma.com/mdb/cpzs_ad321817h_811I7k1.asp");
            if (html.IndexOf("发布成功") != -1)
            {
                html = source.Get("http://www.qqma.com/mdb/cpzs_modordelete.asp", "http://www.qqma.com/mdb/cpzs_ad321817h_811I7k1.asp?info=1");
                string url = Regex.Match(html, "<a href=\"([^\"]+)\" target=\"_blank\">" + title + "</a>").Groups[1].Value;
                if (url.Equals(""))
                {
                    info.Code = 5;
                }
                else
                {
                    info.Code = 1;
                    info.Content = "http://www.qqma.com" + url;
                }
            }
            else if (Regex.IsMatch(html, "您当日发布的产品展示已达到[0-9]*条/日上限"))
            {
                info.Code = 3;
            }
            else if (html.Equals(""))
            {
                info.Code = 6;
            }
            return info;
        }
        private InformationResult PostProductBusinessOpportunities(string title, string content)
        {
            string html = "";
            InformationResult info = new InformationResult();
            SourceFormData sfd = new SourceFormData("gb2312");
            //头部数据
            sfd.Write("lb", "供应");
            sfd.Write("zt", title);
            sfd.Write("yxq", "365");
            sfd.Write("RootCategory", site.TypeId[0]);
            sfd.Write("sshy", site.TypeId[1]);
            sfd.Write("nr", MyFunction.ToText(content));
            sfd.Write("tag", ktag.Get(3, " "));
            //发送文件
            sfd.WriteFile("file1", RandomPicture.Get());
            //尾部数据
            sfd.Write("submitPost", "一切完成，我要发布！");
            //结尾
            html = source.Post("http://www.qqma.com/mdb/syjh_asdavIe2a1bO7_w.asp", sfd.GetContentType(), sfd.Read(), "http://www.qqma.com/mdb/syjh_ad128b8_8_1&x5.asp");
            if (html.IndexOf("<font color=\"#FF0000\">恭喜，您的信息已经成功提交，您可在下面继续添加信息") != -1)
            {
                html = source.Get("http://www.qqma.com/mdb/syjh_modordelete.asp", "http://www.qqma.com/mdb/syjh_ad128b8_8_1&x5.asp");
                string url = Regex.Match(html, "<a href=\"(/Supply/[0-9]+\\.html)\" target=\"_blank\">" + title + "</a>").Groups[1].Value;
                if (url.Equals(""))
                {
                    info.Code = 5;
                }
                else
                {
                    info.Code = 1;
                    info.Content = "http://www.qqma.com" + url;
                }
            }
            else if (Regex.IsMatch(html, "您当日发布的商业机会已达到[0-9]*条/日上限"))
            {
                currentID = 1;
                info.Code = 1;
                info.Content = "商业机会发布已满";
            }
            else if (html.Equals(""))
            {
                info.Code = 6;
            }
            return info;
        }
    }
}
